

//商家注册
$(function(){
    var publicKey;
    $("#merchant-register-Brt").click(function(){
        $("#merchant-center #rg1").show().siblings().hide();
    });
    $("#merchant-center #rg-Btr1").click(function(){
        if(!rg1()){
            return;
        }
        $("#merchant-center #rg2").show().siblings().hide();
    });
    $("#merchant-center #rg-Btr2").click(function(){
        if(!rg2()){
            return;
        }
        $("#merchant-center #rg3").show().siblings().hide();
    });
    $("#merchant-center #rg-Btr3").click(function(){
        if(!rg3()){
            return;
        }
        $("#merchant-center #rg4").show().siblings().hide();
    });
    $("#merchant-center #rg-Btr4").click(function(){
        if(!rg4()){
            return;
        }
        $("#merchant-center #rg5").show().siblings().hide();
    });
    $("#merchant-center #rg-Btr5").click(function() {

        if (!rg5()) {
            return;
        }

        //获取参数
        var par = {};
        var data = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_ADD_NEW_MERCHANT";
        //签名算法
        par.secId = "RSA";
        //接口版本
        par.version = "4.0.0";
        par.merchantMail = $("#merchant-center input[name=merchantMail]").val();
        // 商户邮箱
        data.merchantMail = $("#merchant-center input[name=merchantMail]").val();
        //邮箱验证码
        data.mailCode = $("#merchant-center input[name=mailCode]").val();
        // 应用名称
        data.appName = $("#merchant-center input[name=appName]").val();
        //手机号
        data.mobileNo = $("#merchant-center input[name=mobileNo]").val();
        // 接入密码
        data.accessPassword = $("#merchant-center input[name=accessPassword]").val();
        //负责人姓名
        data.principalName = $("#merchant-center input[name=principalName]").val();
        //负责人身份证号
        data.idCardNumber = $("#merchant-center input[name=idCardNumber]").val();
        // 身份证正面
        par.IdCardFront = $("#merchant-center img[name=IdCardFront]").attr("src");
        // 身份证反面
        par.IdCardNegative = $("#merchant-center img[name=IdCardNegative]").attr("src");
        // 银行卡号
        data.bankCardNo = $("#merchant-center input[name=bankCardNo]").val();
        // 归属银行
        data.bankName = $("#merchant-center input[name=bankName]").val();
        // 业务数据
        // console.log(data);

        // console.log(publicKey);
        // console.log(data);
        var dat = JSON.stringify(data);
        var rsaData = fenduanjiami(publicKey, dat, 58);
        // console.log(rsaData);
        par.data = rsaData.join("");

        // console.log(par);
        // $ajax(JSON.stringify(par));

        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: true,
            beforeSend: function () {
                //加载动画
                $(".loading").append('<span class="ant-spin-dot ant-spin-dot-spin"><i></i><i></i><i></i><i></i></span>');
                //按钮不可点击
                $("#rg-Btr5").attr("disabled", true);
                $("#rg-Btr5").css("backgroundColor", "#c1c5de");
            },
            success: function (result) {
                if (result.respCode == "00") {
                    $("#merchant-center #rg6").show().siblings().hide();
                    new invokeSettime("#msg-rg-ok");
                    $("input[type=text]").val("");
                    $("input[type=password]").val("");
                } else {
                    alert(result.respMsg);
                }
            }
        });
    });
    //关闭按钮
    $("#merchant-center .cancel").click(function(){
        $("#merchant-center .container").show().siblings().hide();
        window.location.reload()
        // $("input[type=text]").val("");
    });
    //获取邮箱验证码
    $("#merchant-center #rg1 input[name=getCode]").click(function() {
        //获取参数
        var par = {};
        var data = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_RESEND_SMS_VERIFICATION_CODE";
        //签名算法
        par.secId = "RSA";
        //接口版本
        par.version = "4.0.0";
        par.merchantMail = $("#merchant-center input[name=merchantMail]").val();
        // 商户邮箱
        data.merchantMail = $("#merchant-center input[name=merchantMail]").val();
        //订单号（注册默认1000010）
        data.outOrderId = "1000010";
        //方法版本
        data.methodVer = "3.0";

        var mobileNo = $("#merchant-center input[name=mobileNo]").val();

        var option = {
            "service": "NEW_APE_WANG_API3_GET_THE_PUBLIC_KEY",
            "version": "4.0.0",
            "mobileNo": mobileNo,
            "merchantMail": data.merchantMail

        };
        // console.log(option);
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(option),
            dataType: "json",
            contentType: "application/json",
            async: false,
            success: function (result) {
                publicKey = result.publicKey;
            }
        });
        // $ajax(JSON.stringify(option));
        // publicKey = value.publicKey;

        var dat = JSON.stringify(data);
        var rsaData = fenduanjiami(publicKey, dat, 58);

        par.data = rsaData.join("");
        // $ajax(JSON.stringify(par));
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: false,
            success: function (result) {
                if (result.respCode == "00") {

                    new codeSettime($("#merchant-center #rg1 input[name=getCode]"));
                } else {
                    alert(result.respMsg)
                }
            }
        })
    })
});


//个人支付
$(function(){
    var publicKeys;
    $("#prepayment-btr").click(function(){
        $(".prepayment").show().siblings().hide();
        //获取参数
        var par = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_GET_ALL_BUSINESS_INFORMATION";
        //接口版本
        par.version = "4.0.0";
        //数据
        par.data="";
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: true,
            success: function (result) {
                if (result.respCode == "00") {
                    var data = result.data;
                    for (var i=0; i<data.length; i++) {
                        var merchanList = $('.prepayment .merchantId-drop');
                        merchanList.append(
                            '<option value=' + data[i].merchantId + '' + '>' +
                            data[i].appName +
                            '</option>'
                        )
                    }
                }
            }
        })
    });
    $(".prepayment #rg-Btr1").click(function(){
        if(!rg1()){
            return;
        }
        $(".prepayment #rg2").show().siblings().hide();
    });
    $(".prepayment #rg-Btr2").click(function(){
        if(!rg2()){
            return;
        }
        $("#rg3").show().siblings().hide();
    });
    $(".prepayment #rg-Btr3").click(function(){
        if(!rg3()){
            return;
        }
        $(".prepayment #rg4").show().siblings().hide();
    });
    $(".prepayment #rg-Btr4").click(function(){
        if(!rg4()){
            return;
        }
        $(".prepayment #rg5").show().siblings().hide();
    });
    $(".prepayment #rg-Btr5").click(function() {

        if (!rg5()) {
            return;
        }

        //获取参数
        var par = {};
        var data = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_PREPARE_PAY";
        //签名算法
        par.secId = "RSA";
        //接口版本
        par.version = "4.0.0";
        //商户号
        par.merchantId = $(".prepayment .merchantId-drop option:selected").val();
        // 商户邮箱
        data.merchantMail = $(".prepayment input[name=merchantMail]").val();
        //邮箱验证码
        //data.mailCode = $(".prepayment input[name=mailCode]").val();
        //手机号
        data.mobileNo = $(".prepayment input[name=mobileNo]").val();
        // 接入密码
        //data.accessPassword = $(".prepayment input[name=accessPassword]").val();
        //姓名
        data.realName = $(".prepayment input[name=principalName]").val();
        //身份证号
        data.cardNo = $(".prepayment input[name=idCardNumber]").val();
        // 身份证正面
        par.IdCardFront = $(".prepayment img[name=IdCardFront]").attr("src");
        // 身份证反面
        par.IdCardNegative = $(".prepayment img[name=IdCardNegative]").attr("src");
        // 银行卡号
        data.bankCardNo = $(".prepayment input[name=bankCardNo]").val();
        // 归属银行
        //data.bankName = $(".prepayment input[name=bankName]").val();
        //金额
        data.amount= $(".prepayment input[name=amount]").val();
        //回调地址
        data.noticeUrl= "";
        //方法版本
        data.methodVer = "3.0";
        //关单时间
        data.closeOrderTime = "10";
        //备注字段
        data.memo = "";

        // console.log(data);
        var dat = JSON.stringify(data);
        var rsaData = fenduanjiami(publicKeys, dat, 58);
        // console.log(rsaData);
        par.data = rsaData.join("");

        // console.log(par);
        // $ajax(JSON.stringify(par));

        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: true,
            beforeSend: function () {
                //加载动画
                $(".prepayment .loading").append('<span class="ant-spin-dot ant-spin-dot-spin"><i></i><i></i><i></i><i></i></span>');
                //按钮不可点击
                $(".prepayment #rg-Btr5").attr("disabled", true);
                $(".prepayment #rg-Btr5").css("backgroundColor", "#c1c5de");
            },
            success: function (result) {
                if (result.respCode == "00") {
                    $(".prepayment #rg6").show().siblings().hide();
                    var amount = data.amount
                    var outOrderId=result.data.outOrderId;
                    $(".prepayment .pay-msg").append("<p>您正在支付订单:" + outOrderId + ",金额:" + amount + ",邮箱验证码将在10分钟后失效</p>");
                    $("#confirmPay").click(function(){
                        if(!mailCodePay()){
                            return;
                        }
                        confirmPay(outOrderId);

                    });
                } else {
                    alert(result.respMsg);
                }
            }
        });
    });
    //确认支付
    function confirmPay(outOrderId){
        // $("#confirmPay").click(function(){

        //获取参数
        var par = {};
        var data = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_CONFIRM_PAYMENT";
        //签名算法
        par.secId = "RSA";
        //接口版本
        par.version = "4.0.0";
        //商户号
        par.merchantId = $(".prepayment .merchantId-drop option:selected").val();
        //邮箱验证码
        data.mailCode = $(".prepayment input[name=mailCodePay]").val();
        //方法版本
        data.methodVer = "3.0";
        //订单号
        data.outOrderId = outOrderId;
        // console.log(data)
        var da= JSON.stringify(data);
        // console.log(da)
        // console.log(publicKeys)
        var rsaData = fenduanjiami(publicKeys, da,58);
        // console.log(rsaData);
        par.data = rsaData.join("");

        // $ajax(JSON.stringify(par));
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: false,
            success: function (result) {
                if(result.respCode=="00"){
                    alert("支付成功");
                    window.location.reload()
                    $("input[type=text]").val("");
                    $("input[type=password]").val("");
                }else{
                    alert(result.respMsg)
                }
            }
        });

        // });
    }
    //取消支付
    $(".prepayment #cancelPay").click(function(){
        $(".prepayment .container").show().siblings().hide();
        window.location.reload()
        $("input[type=text]").val("");
        $("input[type=password]").val("");
    });

    //关闭按钮
    $(".prepayment .cancel").click(function(){
        $(".prepayment .container").show().siblings().hide();
        window.location.reload()
        $("input[type=text]").val("");
        $("input[type=password]").val("");
    });
    //获取邮箱验证码
    $(".prepayment #rg1 input[name=getCode]").click(function(){
        //获取参数
        var par = {};
        var data = {};
        //接口名称
        par.service = "NEW_APE_WANG_API3_RESEND_SMS_VERIFICATION_CODE";
        //签名算法
        par.secId = "RSA";
        //接口版本
        par.version = "4.0.0";
        par.merchantId = $(".merchantId-drop option:selected").val();
        // 商户邮箱
        data.merchantMail = $(".prepayment input[name=merchantMail]").val();
        //订单号（注册默认1000010）
        data.outOrderId = "1000010";
        //方法版本
        data.methodVer = "3.0";

        // var merchantId= $(".merchantId-drop option:selected").val();
        // console.log(par.merchantId)
        var option = {
            "service": "NEW_APE_WANG_API3_GET_THE_PUBLIC_KEY",
            "merchantId": par.merchantId
        };
        // $ajax(JSON.stringify(option));
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(option),
            dataType: "json",
            contentType: "application/json",
            async: false,
            success: function (result) {
                publicKeys = result.publicKey;
            }
        });
        // console.log(publicKeys)


        var dat = JSON.stringify(data);
        var rsaData = fenduanjiami(publicKeys, dat, 58);

        par.data = rsaData.join("");
        // $ajax(JSON.stringify(par));
        $.ajax({
            type: "POST",
            url: "/mobile/gateway.action",
            data: JSON.stringify(par),
            dataType: "json",
            contentType: "application/json",
            async: false,
            success: function (result) {
                if(result.respCode=="00"){

                    new codeSettime($(".prepayment #rg1 input[name=getCode]"));
                }else{
                    alert(result.respMsg)
                }
            }
        })

    })
});

//查询
$(function(){
    var publicKeys;
    $("#inquire-btr").click(function(){
        $(".inquire").show().siblings().hide()
    });
    $(".cancel-before").click(function(){
        window.location.reload()
    });
    // $(".order-status-btr").click(function(){
    //     $(".inquire>.order-status").show().siblings().hide()
    // });
    $(".order-status-btr").click(function(){
        $(".inquire-table>.order-status").show().siblings().hide()
        $(".order-status-btr").addClass("active").siblings().removeClass("active");
        $("#inquire-order-btr").click(function(){
            $(".order-detail").empty();
            // 获取公钥
            var mobileNo= $(".order-status input[name=mobileNo]").val();
            var merchantMail = $(".order-status input[name=merchantMail]").val();
            var option = {
                "service": "NEW_APE_WANG_API3_GET_THE_PUBLIC_KEY",
                "mobileNo": mobileNo,
                "merchantMail": merchantMail
            };
            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(option),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    publicKeys = result.publicKey;
                }
            });

            //获取参数
            var par = {};
            var data = {};
            //接口名称
            par.service = "NEW_APE_WANG_API3_ORDER_STATUS";
            //签名算法
            par.secId = "RSA";
            //接口版本
            par.version = "4.0.0";
            //商户号
            par.merchantId = $(".order-status input[name=merchantId]").val();
            //订单号
            data.outOrderId = $(".order-status input[name=outOrderId]").val();

            var dat = JSON.stringify(data);
            var rsaData = fenduanjiami(publicKeys, dat, 58);
            par.data = rsaData.join("");


            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(par),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    if(result.outOrderId){
                        $(".order-detail").append(
                            "<p>"+
                            "商户:"+ result.merchantId +",<br>"+
                            "订单号:"+ result.outOrderId +",<br>"+
                            "状态:"+ result.orderStatus +
                            "</p>"
                        )
                    }else {
                        alert(result.respMsg)
                    }
                }
            });

        })

    });
    $(".card-bin-btr").click(function(){
        $(".inquire-table>.card-bin").show().siblings().hide()
        $(".card-bin-btr").addClass("active").siblings().removeClass("active");
        $("#inquire-card-btr").click(function() {
            $(".card-bin-detail").empty();
            // 获取公钥
            var mobileNo = $(".card-bin input[name=mobileNo]").val();
            var merchantMail = $(".card-bin input[name=merchantMail]").val();
            var option = {
                "service": "NEW_APE_WANG_API3_GET_THE_PUBLIC_KEY",
                "mobileNo": mobileNo,
                "merchantMail": merchantMail
            };
            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(option),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    publicKeys = result.publicKey;
                }
            });

            //获取参数
            var par = {};
            var data = {};
            //接口名称
            par.service = "NEW_APE_WANG_API3_QUERY_CARD_BIN";
            //签名算法
            par.secId = "RSA";
            //接口版本
            par.version = "4.0.0";
            //商户号
            par.merchantId = $(".card-bin input[name=merchantId]").val();
            //银行卡号
            data.bankCardNo = $(".card-bin input[name=bankCardNo]").val();

            var dat = JSON.stringify(data);
            var rsaData = fenduanjiami(publicKeys, dat, 58);
            par.data = rsaData.join("");


            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(par),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    if (result.respCode == "00") {
                        // console.log(result)
                        var data=result.data;
                        $(".card-bin-detail").append(
                            "<table>" +
                            "<tr>" +
                            "<td>银行编码</td>" +
                            "<td>银行名称</td>" +
                            "<td>银行卡号</td>" +
                            "<td>卡类型</td>" +
                            "</tr>" +
                            "<tr>" +
                            "<td>" +data.bankCode+ "</td>" +
                            "<td>" +data.bankName+ "</td>" +
                            "<td>" +data.bankCardNo+ "</td>" +
                            "<td>" +data.cardType+ "</td>" +
                            "</tr>" +
                            "</table>"
                        );
                    } else {
                        alert(result.respMsg)
                    }
                }
            });
        });

    });
    $(".bank-list-btr").click(function(){
        $(".inquire-table>.bank-list").show().siblings().hide();
        $(".bank-list-btr").addClass("active").siblings().removeClass("active")
        $("#inquire-bank-btr").click(function() {
            $(".bank-list-detail").empty();
            //获取参数
            var par = {};
            //接口名称
            par.service = "NEW_APE_WANG_API3_BANK_CARD_LIST";
            //签名算法
            par.secId = "RSA";
            //接口版本
            par.version = "4.0.0";
            //商户号
            par.merchantId = $(".bank-list input[name=merchantId]").val();
            //数据
            par.data="";
            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(par),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    if (result.respCode == "00") {
                        // console.log(result)

                        $(".bank-list-detail").append(
                            "<div>" +
                            "<h5>所支持银行如下：</h5>"+
                            "</div>"
                        );
                        var data=result.data;
                        for(var i=0;i<data.length;i++){
                            // var dail= $("<p></p>");
                            // dail.append("<td>" +data[i].bankName+ "</td>");
                            $(".bank-list-detail>div").append("<span>" +data[i].bankName+ "</span>");
                        }
                    } else {
                        alert(result.respMsg)
                    }
                }
            });
        });

    });
    $(".merchant-list-btr").click(function(){
        $(".inquire-table>.merchant-list").show().siblings().hide();
        $(".merchant-list-btr").addClass("active").siblings().removeClass("active")
        $("#inquire-merchant-btr").click(function() {
            $(".merchant-list-detail").empty();
            //获取参数
            var par = {};
            //接口名称
            par.service = "NEW_APE_WANG_API3_GET_ALL_BUSINESS_INFORMATION";
            //接口版本
            par.version = "4.0.0";
            //数据
            par.data="";
            $.ajax({
                type: "POST",
                url: "/mobile/gateway.action",
                data: JSON.stringify(par),
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function (result) {
                    if (result.respCode == "00") {
                        // $(".merchant-list-detail").append(
                        //     "<div>" +
                        //     "<h5>所支持银行如下：</h5>"+
                        //     "</div>"
                        // );
                        $(".merchant-list-detail").append(
                            "<table>" +
                            "<thead>" +
                            "<tr>" +
                            "<td>应用名称</td>" +
                            "<td>商户号</td>" +
                            "</tr>" +
                            "</thead>" +
                            "<tbody>" +
                            "</tbody>" +
                            "</table>"
                        );
                        var data=result.data;
                        for(var i=0;i<data.length;i++){
                            var dail= $("<tr></tr>");
                            dail.append("<td>" +data[i].appName+ "</td>");
                            dail.append("<td>" +data[i].merchantId+ "</td>");
                            $(".merchant-list-detail tbody").append(dail);
                        }

                    } else {
                        alert(result.respMsg)
                    }
                }
            });
        });

    });
});